package com.edu.controller;

import com.edu.bean.User;
import com.edu.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;

@Controller
public class UserController {
    @Resource
    private UserService userService;
    @RequestMapping("/regist")
    public ModelAndView regis(User user){
        ModelAndView mv = new ModelAndView();
        if(userService.userRegister(user) == -1){
            mv.setViewName("regist.jsp");
            mv.addObject("msg","用户名已存在");
        }else{
            mv.setViewName("regist.jsp");
            mv.addObject("msg","注册成功");
        }
        return mv;
    }


    @PostMapping("/login")
    public ModelAndView login(User user){
        ModelAndView mv = new ModelAndView();
        int result = userService.userLogin(user);
        if(result == -1){
            mv.setViewName("log.jsp");
            mv.addObject("msg","用户名不存在");
        }
        if(result == 0){
            mv.setViewName("log.jsp");
            mv.addObject("msg","密码错误");
        }
        if(result == 1){
            mv.setViewName("/index");
        }
        return mv;
    }
}
